package com.roy.comet;

public class CometHashStrategy implements CometStrategy {

	private int dbNum;
	private int tableNum;

	public CometHashStrategy() {

	}

	public CometHashStrategy( int dbNum, int tableNum) {
		this.dbNum = dbNum;
		this.tableNum = tableNum;
	}

	@Override
	public int getNodeNo(Object splitKey) {
		return getDbNo(splitKey) / dbNum;
	}

	@Override
	public int getDbNo(Object splitKey) {
		return getTableNo(splitKey) / tableNum;
	}

	@Override
	public int getTableNo(Object splitKey) {
		int hashCode = calcHashCode(splitKey);
		return hashCode % ( dbNum * tableNum);
	}

	private int calcHashCode(Object splitKey) {
		int hashCode = splitKey.hashCode();
		if (hashCode < 0)
			hashCode = -hashCode;
		return hashCode;
	}

	public static void main(String[] args) {
		CometStrategy cometStrategy = new CometHashStrategy(1,4);
		System.out.print("tableNo："+cometStrategy.getTableNo(5));
		System.out.print("========dbNo："+cometStrategy.getDbNo(5));
	}
}
